package com.abocode.aichat.core.application.impl;

import com.abocode.aichat.configuration.AiChatProperties;
import com.abocode.aichat.data.ReqMessageBody;
import com.abocode.aichat.util.ValidateUtils;
import com.abocode.aichat.validator.UserValidator;
import com.abocode.aichat.data.EntityImpl;
import com.abocode.aichat.exception.EntityFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author guanxf
 * @date 2015/8/5
 */

@Service
public class UserValidatorService implements UserValidator {
    @Autowired
    private AiChatProperties aiChatProperties;
    @Override
    public boolean validate(Object o) {
        boolean isPass = true;
        ReqMessageBody req= (ReqMessageBody) o;
        String uid = req.getUid();
        if (!ValidateUtils.isLegitimacyNo(uid)) {
            isPass = false;
        }
        return isPass;
    }

    @Override
    public EntityImpl entity(Object o) {
        ReqMessageBody req= (ReqMessageBody) o;
        String uid =req.getUid();
        String os = "os";
        String presence = "presence";
        String domain=aiChatProperties.getDomain();
        if (StringUtils.isEmpty(domain)) {
            throw new EntityFormatException("domain not config");
        }
        EntityImpl entity = EntityImpl.parse(uid + EntityImpl.CHAR_AT + domain + EntityImpl.CHAR_SLASH + os);
        entity.setPresence(presence);
        return entity;
    }

}
